/**
 * 
 */
package com.android.weibogo;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.ViewParent;
import android.widget.Checkable;
import android.widget.CheckedTextView;
import android.widget.LinearLayout;
import android.widget.ListView;

/**
 * @author Administrator
 *
 */
public class CheckableLinearLayout extends LinearLayout implements Checkable {

	private CheckedTextView mCheckedTextView;
    private final Drawable mCheckDrawable;
    private final Drawable mRadioDrawable;
    private boolean mIsChecked;

	public CheckableLinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
        TypedArray typedArray = null;

        // Cache the check box drawable.
        typedArray = getContext().getTheme().obtainStyledAttributes(new int[] {android.R.attr.listChoiceIndicatorMultiple});

        if ((typedArray != null) && (typedArray.length() > 0)) {
            mCheckDrawable = typedArray.getDrawable(0);
        }
        else {
            // Fallback if the target theme doesn't define a check box drawable.
            // Perhaps an application specific drawable should be used instead of null.
            mCheckDrawable = null;
        }

        // Cache the radio button drawable.
        typedArray = getContext().getTheme().obtainStyledAttributes(new int[] {android.R.attr.listChoiceIndicatorSingle});

        if ((typedArray != null) && (typedArray.length() > 0)) {
            mRadioDrawable = typedArray.getDrawable(0);
        }
        else {
            // Fallback if the target theme doesn't define a radio button drawable.
            // Perhaps an application specific drawable should be used instead of null
            mRadioDrawable = null;
        }

        // Careful with resources like this, we don't need any memory leaks.
        typedArray.recycle();

        mIsChecked = false;
	}

	/* (non-Javadoc)
	 * @see android.widget.Checkable#isChecked()
	 */
	@Override
	public boolean isChecked() {
		// TODO Auto-generated method stub
		return mIsChecked;

	}

	/* (non-Javadoc)
	 * @see android.widget.Checkable#setChecked(boolean)
	 */
	@Override
	public void setChecked(boolean checked) {
		// TODO Auto-generated method stub
        mIsChecked = checked;

        if (mCheckedTextView != null) {
            mCheckedTextView.setChecked(mIsChecked);
        }
	}

	/* (non-Javadoc)
	 * @see android.widget.Checkable#toggle()
	 */
	@Override
	public void toggle() {
		// TODO Auto-generated method stub
		setChecked(!mIsChecked);
	}

    /* (non-Javadoc)
	 * @see android.view.View#onAttachedToWindow()
	 */
	@Override
	protected void onAttachedToWindow() {
		// TODO Auto-generated method stub
		super.onAttachedToWindow();
	      // Check if there is a valid GUI element that can visualize the current check-state.
        if (mCheckedTextView != null) {
            ViewParent p = getParent();

            // Check if the parent of this list item is a ListView
            if (p instanceof ListView) {
                int choiceMode = ((ListView) p).getChoiceMode();

                // Decide which check-state notation to visualize (check box, radio button or none).
                switch (choiceMode) {
                    case ListView.CHOICE_MODE_MULTIPLE:
                        mCheckedTextView.setCheckMarkDrawable(mCheckDrawable);
                        break;

                    case ListView.CHOICE_MODE_SINGLE:
                        mCheckedTextView.setCheckMarkDrawable(mRadioDrawable);
                        break;

                    default:
                        mCheckedTextView.setCheckMarkDrawable(null);
                        break;
                }
            }
        }
	}

	/* (non-Javadoc)
	 * @see android.view.View#onFinishInflate()
	 */
	@Override
	protected void onFinishInflate() {
		// TODO Auto-generated method stub
		super.onFinishInflate();
		mCheckedTextView = (CheckedTextView) findViewById(android.R.id.text1);
	}

}
